import pandas as pd
from utilities.plot_correlation import plot_distribution_by_col1, plot_histograms

# 读取CSV文件
file_path = r'F:\Personal\data\行情数据\15_min_analysis.csv' # 请替换为你的实际文件路径
df = pd.read_csv(file_path)

# 转换日期列为日期类型
df['trading_date'] = pd.to_datetime(df['trading_date'])

# 按日期分组
grouped = df.groupby(df['trading_date'].dt.date)

# 将 up_ratio 列转换为浮点数，忽略无法转换的值
df['up_ratio'] = pd.to_numeric(df['up_ratio'], errors='coerce')
#
# # 初始化结果
# result_1 = []
# result_2 = []
# result_3 = []
# result_4 = []
#
# # 遍历每个分组
# for date, group in grouped:
#     # # 检查条件1
#     # if any(group['up_ratio'] > 0.7) and any(group['up_ratio'] < 0.5):
#     #     result_1.append(date)
#     #
#     # # 检查条件2
#     # if any(group['up_ratio'] < 0.3) and any(group['up_ratio'] > 0.5):
#     #     result_2.append(date)
#
#     # 检查条件一：找出每个日期中最小的 up_ratio，然后排序取前20个日期
#     max_up_ratio = group['up_ratio'].max()
#
#     min_up_ratio = group['up_ratio'].min()
#     if max_up_ratio > 0.7 and min_up_ratio < 0.5:
#         result_1.append((date, min_up_ratio))
#
#     # 检查条件二：找出每个日期中最大的 up_ratio，然后排序取前20个日期
#     if min_up_ratio < 0.3 and max_up_ratio > 0.5:
#         result_2.append((date, max_up_ratio))
#
#     # 检查条件3：在同一个日期下，一直 > 0.7
#     if all(group['up_ratio'] > 0.7):
#         result_3.append(date)
#
#     # 检查条件4：在同一个日期下，一直 < 0.3
#     if all(group['up_ratio'] < 0.3):
#         result_4.append(date)
#
#
# # 对结果按照要求排序
# result_1.sort(key=lambda x: x[1])  # 按最小 up_ratio 升序排序
# result_2.sort(key=lambda x: x[1], reverse=True)  # 按最大 up_ratio 降序排序
#
# # 提取前20个日期
# top_20_result_1 = [date for date, ratio in result_1[:20]]
# top_20_result_2 = [date for date, ratio in result_2[:20]]

time_list = [
'9:45:00'
,'10:00:00'
,'10:15:00'
,'10:30:00'
,'10:45:00'
,'11:00:00'
,'11:15:00'
,'11:30:00'
,'13:15:00'
,'13:30:00'
,'13:45:00'
,'14:00:00'
,'14:15:00'
,'14:30:00'
,'14:45:00'
,'15:00:00'
]

for time_item in time_list:

    # 筛选出符合条件的trading_date
    filtered_dates = df[(df['trading_timestamp'] == time_item) & (df['up_ratio'] <0.3 )]['trading_date'].unique()

    # 根据筛选出的trading_date过滤出所有数据
    filtered_df = df[df['trading_date'].isin(filtered_dates)]

    plot_histograms(filtered_df, ["up_ratio"])

# print(type(df['up_ratio'][0]))

# plot_distribution_by_col1(df, 'trading_timestamp', 'up_ratio', False)
# # 打印结果
# print("满足条件1 (每个日期中最小的 up_ratio > 0.7 和 < 0.5) 的前20个日期:")
# for date in top_20_result_1:
#     print(date)
#
# print("\n满足条件2 (每个日期中最大的 up_ratio < 0.3 或 > 0.5) 的前20个日期:")
# for date in top_20_result_2:
#     print(date)
#
# print("满足条件3 (在同一个日期下，一直 > 0.7) 的日期:")
# for date in result_3:
#     print(date)
#
# print("满足条件4 (在同一个日期下，一直 < 0.3) 的日期:")
# for date in result_4:
#     print(date)